package com.llpy.controller;


import com.llpy.Service.ApiSlideService;
import com.llpy.Service.CommentService;
import com.llpy.Service.ManageService;
import com.llpy.dto.SlideDto;
import com.llpy.dto.SongDto;
import com.llpy.pojo.Comment;
import com.llpy.pojo.Slide;
import com.llpy.pojo.Song;
import com.llpy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class ManageController {
    @Autowired
    private ManageService manageService;
    @Autowired
    private CommentService commentService;

    @Autowired
    private ApiSlideService apiSlideService;

    //获得所有用户
    @PostMapping("/getAllUser")
    public List<User> getAllUser(){
        return manageService.getAllUser();
    }

    //模糊查询用户
    @PostMapping("/getUserOrNick")
    public Page<User> getUserOrNick(@RequestBody Map user){
        String name1 = user.get("username").toString();
        Pageable pageable = PageRequest.of((Integer) user.get("page"), (Integer) user.get("size"));
        return manageService.findByUserRegex(name1,pageable);
    }



    //获得幻灯片链接
    @PostMapping("/getSlide")
    public List<Slide> getSlide(){
        return apiSlideService.findAllSlide();
   }



    //更改幻灯片
    @PostMapping("/updateSlide")
    public List<Slide> updateSlide(@RequestBody List<SlideDto> slides){
        return apiSlideService.updateSlide(slides);
    }




    //更改歌曲状态
    @PostMapping("/updateSong")
    public boolean updateSong(@RequestBody Map id){
        String id1 = (String) id.get("id");
       return manageService.updateSong(id1);
    }

    //修改歌曲
    @PostMapping("/updateSong1")
    public boolean updateSong1(@RequestBody SongDto songDto){
        return manageService.updateSong1(songDto);
    }

    //获得所有推荐歌曲
    @GetMapping("/getSongs")
    public Page<?> getSongs(Pageable pageable){
        return manageService.getSongs(pageable);
    }

    //查询指定歌手或歌曲
    @PostMapping("/getSongOrSinger")
    public List<SongDto> getSongOrSinger(@RequestBody Map name){
        String name1 = name.get("name").toString();
        return manageService.getSongOrSinger(name1);
    }


    //模糊查询歌曲
    @PostMapping("/getSongRegex")
    public Page<Song> getSongRegex(@RequestBody Map name){
        String name1 = name.get("songName").toString();
        Pageable pageable = PageRequest.of((Integer) name.get("page"), (Integer) name.get("size"));
        return manageService.findBySongRegex(name1,pageable);
    }

    //模糊查询歌手
    @PostMapping("/getSingerRegex")
    public Page<SongDto> getSingerRegex(@RequestBody Map name){
        String name1 = name.get("singerName").toString();
        Pageable pageable = PageRequest.of((Integer) name.get("page"), (Integer) name.get("size"));
        return manageService.findBySingerRegex(name1,pageable);
    }


    //查询所有评论
    @PostMapping("/getComment")
    public List<Comment> saveComment(){
        return commentService.getComment();
    }

    //查询指定评论
    @PostMapping("/getComment1")
    public Page<Comment> getComment1(@RequestBody Map content){
        String name1 = content.get("content").toString();
        Pageable pageable = PageRequest.of((Integer) content.get("page"), (Integer) content.get("size"));
        return manageService.findByNameRegex(name1,pageable);
    }

    //更改评论精选
    @PostMapping("/updateComment")
    public void updateComment(@RequestBody Map id){
        String id1 = (String) id.get("id");
        commentService.updateComment(id1);
    }

    //得到全部精选
    @GetMapping("/getCommentt")
    public Page<?> getCommentt(Pageable pageable){  //拿到前端传过来的分页请求
        return commentService.getCommentt(pageable);
    }


    //删除评论
    @PostMapping("/deleteComment")
    public boolean deleteComment(@RequestBody Map id){
        String id1 = (String) id.get("id");
        return commentService.deleteById(id1);
    }
    //批量删除评论
    @PostMapping("deleteComments")
    public boolean deleteComment(@RequestBody List<String> id){  //接收前端传来的集合
        return commentService.deleteById(id);

    }

}
